﻿@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@section styles{
    <style>
    </style>
}

<div class="layui-row layui-col-space10">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">字典名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="typeName" placeholder="" class="layui-input">
                        </div>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="dict-type-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="layui-col-md6">
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="dict-type-table" lay-filter="dict-type-table"></table>
            </div>
        </div>
    </div>
    <div class="layui-col-md6">
        <div class="layui-card">
            <div class="layui-card-body">
                @{ await Html.RenderPartialAsync("_Empty");}
                <table id="dict-data-table" lay-filter="dict-data-table"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="dict-type-toolbar">

    @{
       await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
       {
            MenuKey = SysMenuConstant.MENU_DICTIONARY_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "Add", "新增" } }
        }); 
    }
</script>

<script type="text/html" id="dict-type-bar">
    @{ await Html.RenderPartialAsync("_OperationTpl", new SystemFunctionModel
                    {
                        MenuKey = SysMenuConstant.MENU_DICTIONARY_MANAGE,
                        FunDictionary = new Dictionary<string, string> { { "Update", "编辑" }, { "Transfer", "子项" } }
                    }); }
</script>

<script type="text/html" id="dict-data-toolbar">
    @{await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_DICTIONARY_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "Add", "新增" }, { "Delete", "删除" } }
        }); }
</script>

<script type="text/html" id="dict-data-bar">
    @{ await Html.RenderPartialAsync("_OperationTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_DICTIONARY_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "Update", "编辑" }, { "Delete", "删除" } }
        }); }
</script>

<script type="text/html" id="dict-type-enable">
    <input type="checkbox" value="{{d.Id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="dict-type-enable" {{d.isenable ? 'checked' : '' }}>
</script>

<script type="text/html" id="dict-data-enable">
    <input type="checkbox" value="{{d.Id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="dict-data-enable" {{d.isenable ? 'checked' : '' }}>
</script>

@section scripts{
    <script>
        layui.use(['table', 'form', 'lscom'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;

            let lscom = layui.lscom;
            let MODULE_PATH = "/Systems/Dictionary/";

            let typeCode;
            var istoolbar = $.trim($("#dict-type-bar").html())=="" ? true : false;
            var istoolbar2 = $.trim($("#dict-data-bar").html())=="" ? true : false;
            
            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '字典名称',
                    field: 'dname',
                    align: 'left',
                    width: 140
                },
                {
                    title: '描述',
                    field: 'remark',
                    align: 'center'
                },
                {
                    title: '字典状态',
                    field: 'isenable',
                    align: 'center',
                    templet: '#dict-type-enable'
                },
                {
                    title: '操作',
                    templet: '#dict-type-bar',
                    align: 'center',
                    width: 160,
                    hide:istoolbar
                }
                ]
            ];
            let dataCols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '标签',
                    field: 'dname',
                    align: 'left',
                    width: 120
                },
                {
                    title: '对应值',
                    field: 'dvalue',
                    align: 'center'
                },
                {
                    title: '状态',
                    field: 'enable',
                    align: 'center',
                    templet: '#dict-data-enable'
                },
                {
                    title: '操作',
                    templet: '#dict-data-bar',
                    align: 'center',
                    width: 180,
                    hide:istoolbar2
                }
                ]
            ];
           
            table.render({
                elem: '#dict-type-table',
                url: MODULE_PATH + "GetDictionaryType",
                page: true,
                cols: cols,
                skin: 'line',
                height: 'full-148',
                toolbar: '#dict-type-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            window.renderData = function (pid) {
                typeCode = pid;
                $(".empty").hide();

                table.render({
                    elem: '#dict-data-table',
                    url: MODULE_PATH + "GetDictionary?pid=" + typeCode,
                    page: true,
                    height: 'full-148',
                    cols: dataCols,
                    skin: 'line',
                    toolbar: '#dict-data-toolbar'
                });
            }

            table.on('tool(dict-type-table)', function (obj) {
                if (obj.event === 'Delete') {
                    window.removeType(obj.data['id']);
                } else if (obj.event === 'Update') {
                    window.editType(obj.data['id']);
                } else if (obj.event === 'Transfer') {
                    window.renderData(obj.data['id'])
                }
            });

            table.on('toolbar(dict-type-table)', function (obj) {
                if (obj.event === 'Add') {
                    window.addType();
                } else if (obj.event === 'refresh') {
                    window.refreshType();
                } else if (obj.event === 'Delete') {

                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.Id; });
                    window.removeType(ids.join(','));
                }
            });

            form.on('submit(dict-type-query)', function (data) {
                table.reload('dict-type-table', {
                    where: data.field
                })
                return false;
            });

            form.on('switch(dict-type-enable)', function (data) {
                let waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: MODULE_PATH + 'ModifyState',
                    data: JSON.stringify({ "Id": data.value, "State": (data.elem.checked ? 1 : 0) }),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        lscom.notify.success(data.message);
                        table.reload('dict-type-table');
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });

            });

            window.addType = function () {
                parent.layer.open({
                    type: 2,
                    title: '新增字典类型',
                    shade: 0.1,
                    area: ['500px', '400px'],
                    content: MODULE_PATH + 'CreatDictionaryType',
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }
            window.editType = function (id) {
                parent.layer.open({
                    type: 2,
                    title: '修改字典类型',
                    shade: 0.1,
                    area: ['500px', '400px'],
                    content: MODULE_PATH + 'EditDictionaryType?id=' + id,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }
            window.removeType = function (data) {
                parent.layer.confirm('确定删除吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'DelDictType',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            table.reload('dict-type-table');
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }
            window.refreshType = function () {
                table.reload('dict-type-table');
            }
            window.saveTypeCallback = function (data) {
                parent.layer.close(data.index);
                lscom.notify.success(data.message);
                table.reload('dict-type-table');
            }
            window.addData = function () {
                parent.layer.open({
                    type: 2,
                    title: '新增字典',
                    shade: 0.1,
                    area: ['500px', '450px'],
                    content: MODULE_PATH + 'CreatDictionary?pid=' + typeCode,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }
            window.editData = function (id) {
                parent.layer.open({
                    type: 2,
                    title: '修改字典',
                    shade: 0.1,
                    area: ['500px', '400px'],
                    content: MODULE_PATH + 'EidtDictionary?id=' + id,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }
            window.removeData = function (data) {
                parent.layer.confirm('确定删除吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'DelDict',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            table.reload('dict-data-table');
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }
            window.saveDataCallback = function (data) {
                parent.layer.close(data.index);
                lscom.notify.success(data.message);
                table.reload('dict-data-table');
            }

            table.on('tool(dict-data-table)', function (obj) {
                if (obj.event === 'Delete') {
                    window.removeData(obj.data['id']);
                } else if (obj.event === 'Update') {
                    window.editData(obj.data['id']);
                }
            });
            table.on('toolbar(dict-data-table)', function (obj) {
                if (obj.event === 'Add') {
                    window.addData();
                } else if (obj.event === 'refresh') {
                    window.refreshData();
                } else if (obj.event === 'Delete') {
                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.id; });
                    window.removeData(ids.join(','));
                }
            });

            form.on('submit(dict-data-query)', function (data) {
                data.field.pid = typeCode;
                table.reload('dict-data-table', {
                    where: data.field
                })
                return false;
            });
            form.on('switch(dict-data-enable)', function (data) {
                let waitIndex = parent.layer.load(2);
                lscom.ajax({
                    url: MODULE_PATH + 'ModifyState',
                    data: JSON.stringify({ "Id": data.value, "State": (data.elem.checked ? 1 : 0) }),
                    lsHandleError: false
                }).done(function (data) {
                    if (data.code == 0) {
                        lscom.notify.success(data.message);
                        table.reload('dict-data-table');
                    }
                }).fail(function (message) {
                    lscom.notify.error(message);
                }).always(function () {
                    parent.layer.close(waitIndex);
                });
            });

            window.refreshData = function () {
                table.reload('dict-data-table');
            }
        })
    </script>
}
